package com.aaa.system.service.impl;

import com.aaa.common.entity.User;
import com.aaa.system.dao.UserDao;
import com.aaa.common.vo.Result;
import com.aaa.system.service.UserService;
import com.aaa.system.vo.UserVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @program: aaa-hospital-parent
 * @description:
 * @author: 闫克起
 * @create: 2024-05-20 17:18
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public Result<User> getByName(String name) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", name);
        wrapper.eq("status", 0);
        wrapper.eq("del_flag", 0);
        User user = userDao.selectOne(wrapper);
        if (user == null) {
            return new Result<User>(5000, "账号或密码错误", null);
        }
        return new Result<User>(2000, "登录成功", user);
    }

    @Override
    public Result<Page<User>> selectByCondition(Integer current, Integer pageSize, UserVo userVo) {
        Page<User> page = new Page<>(current, pageSize);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(userVo.getPhone())) {
            wrapper.eq("phone", userVo.getPhone());
        }
        if (StringUtils.hasText(userVo.getUserName())) {
            wrapper.like("user_name", userVo.getUserName());
        }

        if (Objects.nonNull(userVo.getDeptId())) {
            wrapper.eq("dept_id", userVo.getDeptId());
        }
        if (Objects.nonNull(userVo.getStatus())) {
            wrapper.eq("status", userVo.getStatus());
        }

        if (userVo.getDateRange() != null && userVo.getDateRange().size() == 2) {
            wrapper.between("create_time", userVo.getDateRange().get(0), userVo.getDateRange().get(1));
        }

        Page<User> page1 = userDao.selectPage(page, wrapper);
        return new Result<>(2000, "查询成功", page1);
    }

    @Override
    public Result<String> batchDelete(List<Integer> ids) {
        int i = userDao.deleteBatchIds(ids);
        return i > 0 ? new Result<>(2000, "删除成功", null) : new Result<>(5000, "删除失败", null);
    }

    @Override
    public Result<String> batchResetPwd(List<Integer> ids) {
        //1.根据id查询用户信息
        List<User> users = ids.stream().map(id -> {
            return userDao.selectById(id);
        }).collect(Collectors.toList());
        //2.遍历所有
        for (User u : users) {
            Md5Hash pwd = new Md5Hash("123456", u.getSalt(), 5);
            u.setPassword(pwd.toString());
            userDao.updateById(u);
        }
        return new Result<>(2000, "重置成功", null);
    }

    @Override
    public Result<List<User>> selectAll() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("scheduling_flag", 0);
        wrapper.eq("del_flag", 0);
        List<User> users = userDao.selectList(wrapper);
        return new Result<>(2000, "查询所有用户成功", users);
    }

    @Override
    public Result<List<User>> getScheduling(Map<String, Object> map) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("scheduling_flag",0);
        wrapper.eq("del_flag", 0);
        if (Objects.nonNull(map.get("userId"))) {
            wrapper.eq("user_id", map.get("userId"));
        }
        if (Objects.nonNull(map.get("deptId"))) {

            wrapper.eq("dept_id", map.get("deptId"));
        }
        List<User> users = userDao.selectList(wrapper);
        return new Result<>(2000, "查询所有用户成功", users);
    }
}















